package com.yssoft.controller.app.inspection;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.DecoderException;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yssoft.entity.core.TCode;
import com.yssoft.entity.inspection.TInspReport;
import com.yssoft.service.inspection.InspReportService;
import com.yssoft.utils.AppUtil;

/**
 * @创建人: 戈超
 * @时间: 2018/5/21 上午 09:31
 * @说明: 检验控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/app/inspReport")
public class InspReportController {
	private static Logger logger=Logger.getLogger(InspReportService.class);
	@Resource
	private InspReportService inspReportService;
	
	/**
	 * 入库申请单列表查询
	 * @author 戈超
	 * @param currentPage
	 * @param pageSize
	 * @param stat
	 * @param pType
	 * @param request
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "queryInstockApplyList", method = RequestMethod.POST)
	public Object queryInstockApplyList(Integer currentPage, Integer pageSize, String inspStat, String pType, HttpServletRequest request){
		String msg = "入库申请列表查询";
    	try{
    		List<Map<String,Object>> pageList = inspReportService.queryInstockApplyList(currentPage, pageSize, inspStat, pType, request);
        	return AppUtil.returnListJson("1", msg+"成功!", pageList);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 查询产品分类列表
	 * @author 戈超
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "queryCode", method = RequestMethod.POST)
	public Object queryCode(){
		String msg = "查询产品分类列表";
    	try{
    		List<TCode> codeList = inspReportService.queryCodeList();
        	return AppUtil.returnListObjectJson("1", msg+"成功!", codeList);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 查询检验项目列表
	 * @author 戈超
	 * @param currentPage
	 * @param pageSize
	 * @param standardName
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "queryStandardList", method = RequestMethod.POST)
	public Object queryStandardList(Integer currentPage, Integer pageSize, String standardName, String pType, String ids, String productId, HttpServletRequest request){
		String msg = "查询检验项目列表";
    	try{
    		List<Map<String,Object>> standardList = inspReportService.queryStandardEntList(currentPage, pageSize, standardName, pType, ids, productId, request);
        	return AppUtil.returnListJson("1", msg+"成功!", standardList);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 添加检验报告
	 * @author 戈超
	 * @param tInspReport
	 * @param tInspStaEId
	 * @param isUp
	 * @param inspValue
	 * @param request
	 * @param files
	 * @return
	 * @throws IOException
	 * @throws DecoderException
	 */
	@RequestMapping(value = "uploadContract", method = RequestMethod.POST)
    public Object uploadContract(TInspReport tInspReport, String[] tInspStaEId, String[] isUp, String[] inspValue, HttpServletRequest request, @RequestParam("files") MultipartFile[] files) throws IOException, DecoderException {
		try {
            return  inspReportService.uploadContract(tInspReport, tInspStaEId, isUp, inspValue, request, files);
        } catch (Exception e) {
            return AppUtil.returnMapJson("0","保存失败"+e.getMessage(),null);
        }
    }
	
	/**
	 * 检验单确认完成
	 * @author 戈超
	 * @param instockId
	 * @param InspStat
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "instockCompletion", method = RequestMethod.POST)
	public Object instockCompletion(Integer instockId, String InspStat){
		String msg = "确认完成";
    	try{
    		inspReportService.instockCompletion(instockId, InspStat);
        	return AppUtil.returnMapJson("1", msg+"成功!", null);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 查看检验报告
	 * @param instockId
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "inspectionView", method = RequestMethod.POST)
	public Object inspectionView(Integer instockId, HttpServletRequest request){
		String msg = "查看检验单";
    	try{
    		List<Map<String,Object>> list = inspReportService.InspectionView(instockId, request);
        	return AppUtil.returnListJson("1", msg+"成功!", list);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
}
